﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Objects</title>
</head>
<body>
    <ul>
        <content>Objects in Javascript</content>
        <li>The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined.    </li>
        <li>All other values are objects. </li>
        <li>Numbers, strings, and booleans are object-like in that they have methods, but they are immutable. </li>
        <li>Objects in JavaScript are mutable keyed collections. </li>
        <li>In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects.</li>
        <li>An object is a container of properties, where a property has a name and a value.</li>
    </ul>
    <script type="text/javascript">
        var objectExtender = function () {
            if (typeof Object.beget !== 'function') {
                Object.beget = function (o) {
                    var F = function () { };
                    F.prototype = o;
                    return new F();
                };
            }
        }();

        //test data definitions
        var empty_object = {};
        var stooge = {
            "first-name": "Jerome",
            "last-name": "Howard",
            testFunction: function () { },
        };
        var another_stooge = Object.beget(stooge); another_stooge.nickname = 'Moe';

        var flight = {
            airline: "Oceanic",
            number: 815,
            departure: {
                IATA: "SYD",
                time: "2004-09-22 14:55",
                city: "Sydney"
            },
            arrival: {
                IATA: "LAX",
                time: "2004-09-23 10:42",
                city: "Los Angeles"
            }
        };

        var objects = {
            aProperty: 'value',
            "Another!!¿?__": 'can have any name if it is defined with quotes',
            accessToProperty: function () {
                var anObject = { id: 1 };
                return anObject["id"] == anObject.id;
            }(),
            defaultValue: {}.NotExistent || 'default',
            avoidTypeError: function () {
                var object = {};
                return (object.NotExistentProperty && object.NotExistentProperty.Id) == undefined;
            }(),
            reflection: function () {
                return flight.hasOwnProperty('number') &&
                    undefined != typeof flight.constructor && //tiene la propiedad
                    !flight.hasOwnProperty('constructor'); //pero no el mismo
            }(),
            gettingAllPropertyNamesAndValuesWithoutFunctions: function () {
                var result = '';
                for (var name in another_stooge) {
                    if (typeof another_stooge[name] !== 'function') {
                        result += name + ': ' + another_stooge[name] + '\t';
                    }
                }
                return result;
            }(),
            gettingAllPropertyNames: function () {
                var result = '';
                for (var name in another_stooge) {
                    result += name + '\t';
                }
                return result;
            }(),
            gettinOnlyMyNames: function () {
                var result = '';
                for (var name in another_stooge) {
                    if (another_stooge.hasOwnProperty(name))
                        result += name + '\t';
                }
                return result;
            }(),
            removingAProperty: function () {
                //delete a property
                var testObj = { id: 1 }
                var ok = testObj.id != undefined;
                delete testObj.id;
                return ok && testObj.id == undefined;
            }(),
        };
    </script>
</body>
</html>
